import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

const render = (container?: HTMLElement) => {
  const appContainer = container ?? document.getElementById('root')
  createRoot(appContainer!).render(
    <StrictMode>
      <App />
    </StrictMode>,
  )
}

// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render()
}

// 作为微应用运行时
renderWithQiankun({
  mount: (props) => {
    console.log('mount')
    render(props.container?.querySelector('#root') as HTMLElement)
  },
  bootstrap: () => {
    console.log('bootstrap')
  },
  unmount: () => {
    console.log('unmount')
  },
  update: (_props) => {  // 修改这里，添加下划线前缀
    console.log('update')
    // 如果需要使用 props，可以取消下面的注释并使用
    // console.log('update props', props)
  }
})